##
# Certificating 是参与者和证书之间的认证关系。

module Unidom
  module Certificate

    class Certificating < ApplicationRecord

      self.table_name = 'unidom_certificatings'

      include Unidom::Common::Concerns::ModelExtension

      belongs_to :certificator,  polymorphic: true
      belongs_to :certificated,  polymorphic: true
      belongs_to :certification, polymorphic: true

      scope :certificated_by,  ->(certificator)  { where certificator:  certificator  }
      scope :certificated_is,  ->(certificated)  { where certificated:  certificated  }
      scope :certification_is, ->(certification) { where certification: certification }

      ##
      # 由认证者 certificator 用证书 certification 去认证的人或者机构 certificated ，认证时间是： opened_at。
      def self.certificate!(certification: nil, certificated: nil, certificator: nil, opened_at: Time.now)
        assert_present! :certification, certification
        assert_present! :certificated,  certificated
        assert_present! :opened_at,     opened_at

        attributes = { opened_at: opened_at }
        if certificator.present?
          attributes[:certificator] = certificator
        else
          attributes[:certificator_id]   = Unidom::Common::NULL_UUID
          attributes[:certificator_type] = ''
        end
        self.certification_is(certification).certificated_is(certificated).valid_at(now: opened_at).alive.first_or_create! attributes
      end

    end unless Unidom::Common::Neglection.namespace_neglected? 'Unidom::Certificate::Certificating'

  end
end
